# -*- coding: utf-8 -*-

import BBS
from BBS import user
from BBS import article
from flask import request, Flask, jsonify

BBS.init_datebase()
app = Flask(__name__)

@app.route("/login")
def login():
    uname = request.args.get("uname")
    pwd = request.args.get("pwd")
    if not (uname and pwd):
        return jsonify({
            "code": -102,
            "msg": "params need"})
    try:
        _user = user.User(uname=uname)
    except:
        return jsonify({
            "code": -100,
            "msg": "user doesn't exist"})
    if pwd == _user.pwd:
        return jsonify({
            "code": 0,
            "data": {
                "aid": _user.uid,
                "name": _user.name}})
    else:
        return jsonify({
            "code": -101,
            "msg": "password wrong"})


@app.route("/register")
def register():
    uname = request.args.get("uname")
    name = request.args.get("name")
    pwd = request.args.get("pwd")
    if not (uname and name and pwd):
        return jsonify({
            "code": -202,
            "msg": "params need"
            })
    _user = user.User()
    _user.uname = uname
    _user.name = name
    _user.pwd = pwd
    if _user.register():
        return jsonify({
            "code": 0,
            "data": {
                "uid": _user.uid,
                "uname": _user.uname,
                "name": _user.name}})
    else:
        return jsonify({
            "code": -201,
            "msg": "uname has been used"
            })


@app.route("/articles")
@app.route("/articles/<int:page>")
def aricles(page=1):
    _articles = article.article_list(page)
    return jsonify({
        "code": 0,
        "data": [{
            "aid": i.aid,
            "title": i.title,
            "author_id": i.author_id,
            "author_name": i.author_name,
            "last_time": i.last_time
        } for i in _articles]
        })


@app.route("/post", methods=["POST"])
def post():
    title = request.form.get("title")
    content = request.form.get("content")
    author_id = request.form.get("author_id")
    _article = article.Article()
    _author = user.User(uid=int(author_id))
    _article.title = title
    _article.content = content
    _article.author_id = _author.uid
    _article.author_name = _author.name
    _article.post()
    return jsonify({
        "code": 0,
        "data": {
            "aid": _article.aid,
            "title": _article.title
        }})


@app.route("/replies")
def replies():
    aid = request.args.get("aid")
    page = request.args.get("page", 1)
    return jsonify({
        "code": 0,
        "data": article.relpy_list(int(aid), int(page))
        })


@app.route("/reply", methods=["POST"])
def reply():
    aid = int(request.form.get("aid"))
    content = request.form.get("content")
    author_id = int(request.form.get("author_id"))
    try:
        _article = article.Article(aid=aid)
    except ValueError:
        return jsonify({
            "code": -301,
            "msg": "aid doesn't exists"})
    try:
        _user = user.User(uid=author_id)
    except ValueError:
        return jsonify({
            "code": -302,
            "msg": "uid doesn't exists"})
    if _article.reply(author_id, content):
        return jsonify({
            "code": 0,
            "data": []})
    else:
        return jsonify({
            "code": -303,
            "msg": "unknown error"})


@app.route("/detail")
def detail():
    aid = int(request.args.get("aid"))
    try:
        _article = article.Article(aid=aid)
    except ValueError:
        return jsonify({
            "code": -401,
            "msg": "aid doesn't exists"})
    return jsonify({
        "code": 0,
        "data": {
            "aid": _article.aid,
            "title": _article.title,
            "content": _article.content,
            "author_id": _article.author_id,
            "author_name": _article.author_name,
            "last_time": _article.last_time
        }})




if __name__ == '__main__':
    app.run("127.0.0.1", port=8888)